package cn.edu.glut.user.controller;

import javax.validation.Valid;

import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.glut.dto.MajorInputDTO;
import cn.edu.glut.dto.Response;
import cn.edu.glut.entity.Major;
import cn.edu.glut.user.service.MajorService;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月13日下午9:06:45
 * @email Caizhfy@163.com
 * @description 专业
 *
 */
@RestController
@RequestMapping("/major")
public class MajorController {
	@Autowired
	MajorService majorService;

	@RequestMapping("/new")
	public Response save(@Valid MajorInputDTO majorInputDTO){
		return Response.success(majorService.save(majorInputDTO.convertToEntity()));
	}
	
	@RequestMapping("/del/{id}")
	public Response delete(@PathVariable String id){
		majorService.delete(id);
		return Response.success();
	}
	
	@RequestMapping("/update")
	public Response update(@Valid MajorInputDTO majorInputDTO){
		Preconditions.checkNotNull(majorInputDTO.getId(), "专业主键不能为空");//NPE
		return Response.success(majorService.update(majorInputDTO.convertToEntity()));
	}
	
	@RequestMapping("/get/{id}")
	public Response get(@PathVariable String id){
		return Response.success(majorService.findById(id));
	}
	
	@RequestMapping("/list")
	public Response list(@PageableDefault Pageable pageable){
		return Response.success(majorService.list(pageable));
	}
}
